package com.chb.service.impl;

import com.chb.dao.MenuDao;
import com.chb.model.entity.Menu;
import com.chb.model.entity.Staff;
import com.chb.model.vo.Result;
import com.chb.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 陈海彬
 * @date: Created in 2023/9/25 16:17
 * @description:
 * @modified By:
 * @version:
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private HttpSession session;

    /**
     * @create by: 陈海彬
     * @description: 查找左侧菜单栏
     * @create time: 2023/9/25 16:47
     */
    @Override
    public Result getLeftMenu() {
        //获得用户
        Staff staff = (Staff) session.getAttribute("staff");
        //从数据库获得菜单
        List<Menu> list = menuDao.getLeftMenu(staff.getStaffid());

        //找到一级菜单
        List<Menu> firsts = new ArrayList<Menu>();
        for (Menu m : list) {
            if (m.getMpid() == 0) {
                firsts.add(m);
            }
        }
        //找到一级菜单对应的二级菜单
        for (Menu f : firsts) {
            List<Menu> children = new ArrayList<Menu>();
            for (Menu m : list) {
                if (m.getMpid().equals(f.getMid())) {
                    children.add(m);
                }
            }
            f.setChildren(children);
        }
        return Result.success(firsts);
    }

    @Override
    public Result selectMidByRoleid(Integer id) {
        //查询所有菜单
        List<Menu> menus = menuDao.selectAllMenu();
        //找到一级菜单
        List<Menu> fatherlist = new ArrayList<Menu>();
        for (Menu m : menus) {
            if (m.getMpid() == 0) {
                fatherlist.add(m);
            }
        }
        //找到一级菜单对应的二级菜单
        for (Menu f : fatherlist) {
            List<Menu> children = new ArrayList<Menu>();
            for (Menu m : menus) {
                if (m.getMpid().equals(f.getMid())) {
                    children.add(m);
                }
            }
            f.setChildren(children);
        }
        //通过角色id查询该角色具有的菜单id ==>mids  "所有菜单" ==>fatherlist
        List<Menu> mids = menuDao.seletMidByRoleid(id);
        //因为无法同时向前端传递 "所有菜单"及"及角色所具有的菜单id"
        //所以将 "所有菜单"及"角色所具有的菜单id" 封装===>MenuMap
        Map<String,Object> MenuMap=new HashMap<>();
        MenuMap.put("fatherlist",fatherlist);
        MenuMap.put("mids",mids);
        return new Result(200,"查询成功",MenuMap);
    }

    /**
     * 通过角色id给角色赋给新的菜单id
     * @param roleId
     * @param ids
     * @return
     */
    @Override
    public Result modifyMidByRoleid(Integer roleId, Integer[] ids) {
        //删除角色本身具有的菜单id
        List<Menu> menus = menuDao.seletMidByRoleid(roleId);
        Integer integer = menuDao.delMidByRoleid(roleId);
        List<Menu> menus1 = menuDao.seletMidByRoleid(roleId);
        //重新给角色赋有新的菜单id
        menuDao.insertNewMidByRoleid(roleId, ids);
        return new Result(200,"修改权限成功",1);
    }
}